//
//  HNSH_NoticeAlert.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/5.
//

#define kConentWidth     239

#import "HNSH_NoticeAlert.h"
#import "HNSH_PopupController.h"

#import "KKImageBrowser.h"

@interface HNSH_NoticeAlert ()

/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;

/*  */
@property (nonatomic, strong) UIScrollView *scrollView;
/* desLabel */
@property (nonatomic, strong) UILabel *desLabel;
/* cancelButton */
@property (nonatomic, strong) UIButton *cancelButton;
/* showButton */
@property (nonatomic, strong) UIButton *showButton;

/* nyPopView */
@property(nonatomic, strong) HNSH_PopupController *nyPopView;

@end


@implementation HNSH_NoticeAlert

- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, 295, 424);
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

- (void)setModel:(HNSH_NoticeAlertModel *)model {
    _model = model;
    self.desLabel.text = model.content;
    CGFloat height = [model.content heightForFont:self.desLabel.font width:kConentWidth];
    if (model.content.length > 0) {
        height = height + 12;
    }
    for (int i = 0; i < model.image_urls.count; i ++) {
        NSString *url = model.image_urls[i];
        CGSize size = [self getImageWidthAndHeightWithImageString:url];
        UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, height, kConentWidth, (kConentWidth * (size.height / size.width)))];
        [imgV sd_setImageWithURL:[NSURL URLWithString:url]];
        imgV.tag = i;
        [self.scrollView addSubview:imgV];
        height = height + (kConentWidth * (size.height / size.width)) + 12;
        
        imgV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
        [imgV addGestureRecognizer:tap];
    }
    self.scrollView.contentSize = CGSizeMake(0, height);
    
    self.titleLabel.text = model.title;
}

- (void)tapGesture:(UITapGestureRecognizer *)tap {
    UIView *view = tap.view;
    NSMutableArray *images = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.model.image_urls.count; i ++) {
        NSString *imageString = self.model.image_urls[i];
        KKImageBrowserModel *model = [[KKImageBrowserModel alloc] initWithURL:[NSURL URLWithString:imageString] toView:kSTWindow];
        [images addObject:model];
    }
    KKImageBrowser *vc = [[KKImageBrowser alloc] init];
    vc.images = images;
    vc.index = view.tag;
    [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:vc animated:NO completion:nil];
}

- (void)showViewWithView:(UIView *)view {
    self.nyPopView = [[HNSH_PopupController alloc]init];
    self.nyPopView.dismissOnMaskTouched = NO;
    self.nyPopView.layoutType = PopupLayoutTypeCenter;
    self.nyPopView.slideStyle = PopupSlideStyleFade;
    [self.nyPopView presentContentView:self duration:0.25 springAnimated:YES inView:view];
    self.nyPopView.popupView.layer.cornerRadius = 16;
    self.nyPopView.popupView.layer.masksToBounds = YES;
}

- (void)closeView {
    [self.nyPopView dismissWithDuration:0.25 springAnimated:YES];
}

- (void)addAllViews {
    UIImageView *topBgView = [[UIImageView alloc]init];
    topBgView.image = [UIImage imageNamed:@"home_gg_alert_bg"];
    [self addSubview:topBgView];
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(180);
    }];
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.scrollView];
    
    [self.scrollView addSubview:self.desLabel];
//    [self addSubview:self.cancelButton];
    [self addSubview:self.showButton];
}

- (void)addAllLayout {
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(20);
        make.height.mas_equalTo(28);
    }];
    
//    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(28);
//        make.right.mas_equalTo(-28);
//        make.top.mas_equalTo(62);
//        make.bottom.mas_equalTo(-80);
//    }];
    
    [self.desLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(kConentWidth);
        make.top.mas_equalTo(0);
    }];
    
    [self.showButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.width.mas_equalTo(148);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-20);
    }];
}

#pragma mark lazy
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"奖励提示";
        _titleLabel.font = [UIFont boldSystemFontOfSize:20];
        _titleLabel.textColor = [UIColor appBlackLabelColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)desLabel {
    if (!_desLabel) {
        _desLabel = [[UILabel alloc]init];
        _desLabel.text = @"您的粉丝追风的少年，于2022-05-03进行了认养，您本次的收益为100.00元，该收益发放到余额，请及时查收";
        _desLabel.font = [UIFont systemFontOfSize:14];
        _desLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _desLabel.textAlignment = NSTextAlignmentCenter;
        _desLabel.numberOfLines = 0;
    }
    return _desLabel;
}

- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor appBlack333LabelColor] forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _cancelButton.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
        _cancelButton.layer.cornerRadius = 22;
        _cancelButton.layer.masksToBounds = YES;
        __weak __typeof(&*self)weakSelf = self;
        [[_cancelButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf closeView];
        }];
    }
    return _cancelButton;
}

- (UIButton *)showButton {
    if (!_showButton) {
        _showButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_showButton setTitle:@"确定" forState:UIControlStateNormal];
        [_showButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        _showButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _showButton.backgroundColor = [UIColor appGreenColor];
        _showButton.layer.cornerRadius = 20;
        _showButton.layer.masksToBounds = YES;
        __weak __typeof(&*self)weakSelf = self;
        [[_showButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (weakSelf.showBlock) {
                weakSelf.showBlock();
            }
            [weakSelf closeView];
        }];
    }
    return _showButton;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(28, 62, kConentWidth, 282)];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(0, 0);
    }
    return _scrollView;
}

- (CGSize)getImageWidthAndHeightWithImageString:(NSString *)string {
    //图片切割尾部
    NSArray *ary1 = [string componentsSeparatedByString:@"_"];
    //获得图片宽x高
    NSArray *ary2 = [ary1[1] componentsSeparatedByString:@"."];
    //获得图片宽高
    NSArray *ary3 = [ary2[0] componentsSeparatedByString:@"x"];
    return CGSizeMake([ary3[0] doubleValue], [ary3[1] doubleValue]);
}

@end
